package xyz.xiangyushui.util.dataBase.client.command;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class CommandParser {
	private final String pattern;
	
	public CommandParser(){
		pattern=".*";
	}
	
	public CommandParser(String pattern) {
		this.pattern=pattern;
	}

	public Command compile(String input){
		Matcher m=Pattern.compile(pattern).matcher(input);
		if(!m.matches())
			throw new IllegalArgumentException("Invalid command:"+input);
		return toCommand(m);
	}
	
	protected abstract Command toCommand(Matcher m);
}
